﻿Imports JetPackTestNavApp1.IsoServiceRef
Imports System.Windows.Data

Partial Public Class Datagrid
    Inherits Page

    Public Sub New()
        InitializeComponent()
    End Sub

    'Executes when the user navigates to this page.
    Protected Overrides Sub OnNavigatedTo(ByVal e As System.Windows.Navigation.NavigationEventArgs)

    End Sub

    Private Sub btnData_Click(ByVal sender As System.Object, ByVal e As System.Windows.RoutedEventArgs) Handles btnData.Click
        Dim client As New IsoServiceRef.IsoServiceClient
        biLoad.IsBusy = True
        With client
            'AddHandler .GetImageFilesCompleted, Sub(s1 As Object, ea As GetImageFilesCompletedEventArgs) dgImages.ItemsSource = ea.Result
            AddHandler .GetImageFilesCompleted, AddressOf PutItemSources
            .GetImageFilesAsync(txtFilename.Text)
            .CloseAsync()
        End With
    End Sub

    Private data As PagedCollectionView
    Private Sub PutItemSources(ByVal s As Object, ByVal e As GetImageFilesCompletedEventArgs)
        data = New PagedCollectionView(e.Result)
        PutItemSources0()
    End Sub

    Private Sub btnSP_Click(ByVal sender As System.Object, ByVal e As System.Windows.RoutedEventArgs) Handles btnSP.Click
        Dim client As New IsoServiceRef.IsoServiceClient
        With client
            AddHandler .GetFilteredImageFilesCompleted, AddressOf PutItemSources2
            .GetFilteredImageFilesAsync(txtFilename.Text)
            .CloseAsync()
        End With
    End Sub

    Private Sub PutItemSources2(ByVal s As Object, ByVal e As GetFilteredImageFilesCompletedEventArgs)
        data = New PagedCollectionView(e.Result)
        PutItemSources0()
    End Sub

    Private Sub PutItemSources0()
        dpImages.Source = data
        dgImages.ItemsSource = data
        biLoad.IsBusy = False
    End Sub

    Private Sub btnDelete_Click(ByVal sender As System.Object, ByVal e As System.Windows.RoutedEventArgs)
        Dim client As New IsoServiceRef.IsoServiceClient
        client.DeleteImageAsync(CType(dgImages.SelectedItem, IsoServiceRef.FileRecord).id)
        MessageBox.Show("Image with id = " & CType(dgImages.SelectedItem, IsoServiceRef.FileRecord).id & " will be deleted")
        data.RemoveAt(dgImages.SelectedIndex)
    End Sub

    Private Sub btnSave_Click(ByVal sender As System.Object, ByVal e As System.Windows.RoutedEventArgs) Handles btnSave.Click
        Dim client As New IsoServiceRef.IsoServiceClient

        With client
            AddHandler .SaveDBchangesCompleted, AddressOf ChangesCompleted
            .SaveDBchangesAsync()
            .CloseAsync()
        End With
    End Sub

    Private Sub ChangesCompleted()
        MessageBox.Show("Changes saved successfully")
    End Sub
End Class
